﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace FUSE
{
    public class Circle
    {

        public Vector2 centerPOS;
        public float radius;

        public Circle()
        {
            centerPOS = Vector2.Zero;
            radius = 0.0f;
        }

        public Circle(Vector2 newCenterPOS, float newRadius)
        {
            centerPOS = newCenterPOS;
            radius = newRadius;
        }

        public void SetPosition(Vector2 newCenterPOS)
        {
            centerPOS = newCenterPOS;
        }

        //booleans 'if intersects with circle'

        public Boolean Intersects(Circle otherCircle)
        {
            double distance = DistanceFromCenter(otherCircle);
            if (distance < (this.radius + otherCircle.radius))
                return true;
            else
                return false;
        }

        public Boolean Intersects(Rectangle rectangle)
        {
            double distance = DistanceFromCenter(rectangle);
            if (distance < radius)
                return true;
            else
                return false;
        }

        public Boolean Intersects(Vector2 pixel)
        {
            double distance = DistanceFromCenter(pixel);
            if (distance < radius)
                return true;
            else
                return false;
        }

        //distances from center of circle

        public double DistanceFromCenter(Circle otherCircle)
        {
            return Vector2.Distance(centerPOS, otherCircle.centerPOS);
        }

        public double DistanceFromCenter(Rectangle rectangle)
        {
            return MinCornerDist(rectangle);
        }

        private double DistanceFromCenter(Vector2 pixel)
        {
            return Vector2.Distance(centerPOS, pixel);
        }



        private double MinCornerDist(Rectangle rectangle)   //find the minimum distance from each corner
        {
            Vector2 Corner1 = new Vector2();    //top left corner
            Corner1.X = rectangle.X;
            Corner1.Y = rectangle.Y;

            Vector2 Corner2 = new Vector2();    //top right corner
            Corner2.X = rectangle.X + rectangle.Width;
            Corner2.Y = rectangle.Y;

            Vector2 Corner3 = new Vector2();    //bottom left corner
            Corner3.X = rectangle.X;
            Corner3.Y = rectangle.Y + rectangle.Height;

            Vector2 Corner4 = new Vector2();    //bottom right corner
            Corner4.X = rectangle.X + rectangle.Width;
            Corner4.Y = rectangle.Y + rectangle.Height;

            double Corner1Dist = DistanceFromCenter(Corner1);
            double Corner2Dist = DistanceFromCenter(Corner2);
            double Corner3Dist = DistanceFromCenter(Corner3);
            double Corner4Dist = DistanceFromCenter(Corner4);

            double minDist = Math.Min((Math.Min(Corner1Dist,Corner2Dist)), Math.Min(Corner3Dist,Corner4Dist));  //take shortest distance to center

            return minDist;

        }
       
    }
}
